package com.eurobcoin.helper;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Random;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class PasswdHelper {

	private static final String ALLOWED_SALT_CHARACTERS = "!$/()='|@#qwertyuiopasdfghjklzxcvbnm,.;:-_{}[]+QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
	
	public static String getSHA512Hash(String s) throws NoSuchAlgorithmException, NoSuchProviderException {
		
		Security.addProvider(new BouncyCastleProvider());
		
		MessageDigest mda = MessageDigest.getInstance("SHA-512", "BC");
		byte [] digesta = mda.digest(s.getBytes());
		
		String ret = String.format("%0128x", new BigInteger(1, digesta));
		
		return ret;
	}
	
	public static String generateSalt() {
		StringBuilder salt = new StringBuilder();
		
		int min = 0;
		int max = ALLOWED_SALT_CHARACTERS.length() - 1;
		
		while(salt.length() < 32) {
			int rnd = showRandomInt(min, max);
			
			char c = ALLOWED_SALT_CHARACTERS.charAt(rnd);
			String s = Character.toString(c);
			
			salt.append(s);
		}
		
		return salt.toString();
	}
	
	public static int showRandomInt(int min, int max) {
		Random rnd = new Random();
		
		int random = rnd.nextInt(max - min + 1) + min;
		
		return random;
	}
	
	public static String generateHash() {
		
		StringBuilder ret = new StringBuilder();
		
		for(int i = 0; i < 32; i++) {
			int random = showRandomInt(65, 90);
			ret.append((char)random);
		}
		
		return ret.toString();
	}
	
}
